package com.zn.customer.controller;

import com.zn.customer.model.Customer;
import com.zn.customer.service.CustomerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/login.do")
public class CurstomerLoginController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //获取服务层对象
        CustomerService customerService = new CustomerService();
        //判断账号密码是否能查询会员对象
        Customer customer = customerService.login(username, password);
        if(customer != null) {
            //获取session对象
            HttpSession session = req.getSession();
            //存储会员编号
            session.setAttribute("customerId",customer.getId());
            //存储用户账号
            session.setAttribute("username",customer.getUser_name());
            req.getRequestDispatcher("product_list.jsp").forward(req, resp);
        }else{
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }
    }
}